
package com.crv.ci.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.crv.ci.model.Group;
import com.crv.ci.service.IGroupService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crv.common.domain.view.BizData4Page;
import com.crv.ci.model.Agent;
import com.crv.ci.service.IAgentService;
import com.crv.common.utils.IdGenerator;
import com.crv.common.utils.JsonResult;
import com.crv.common.utils.PageParam;
import com.crv.common.utils.Constants;

@Controller
public class AgentAction extends AbstractAdminController<IAgentService>{

    @Autowired
    private IAgentService agentService;
    @Autowired
    private IGroupService groupService;
    
    @RequestMapping(value="/agent/list")
    public String list(Agent agent,ModelMap model,PageParam pageParam,HttpServletRequest request,HttpServletResponse response){

        //获取参数
    	Map<String, Object> conditions = getQueryMap(agent); 	
    	BizData4Page<Agent> pageCtx = doPage(model, conditions, pageParam);
    	model.addAttribute("agent", agent);
    	return "/module/agent/list";
    }    
    
    @RequestMapping(value="/agent/add")
    public String add(ModelMap model,HttpServletRequest request,HttpServletResponse response){

        List<Group> groups = groupService.findAll();
        model.addAttribute("gps", groups);
        model.addAttribute("agent", new Agent());
        return "module/agent/edit";
    }    
    
    @RequestMapping(value="/agent/edit")
    public String edit(String id,ModelMap model,HttpServletRequest request,HttpServletResponse response){
    	
    	Agent agent = agentService.fetch(id);
        List<Group> groups = groupService.findAll();
        model.addAttribute("groups", groups);
    	model.addAttribute("agent", agent);
        return "module/agent/edit";
    }    
    
    @RequestMapping(value="/agent/save")
    @ResponseBody
    public String save(HttpServletRequest request,Agent agent){
		boolean isAddNew = StringUtils.isBlank(agent.getId())?true:false;
		if(isAddNew)
		{
			agent.setId(IdGenerator.createNewId());
            agent.setEnable(false);
			agentService.add(agent);
		}
		else
		{
			agentService.update(agent);
		}
        return JsonResult.saveSuccessClose(Constants.MAIN_TAB).toString();
    }    
    
    @RequestMapping(value="/agent/delete")
    @ResponseBody
    public String delete(String ids,HttpServletRequest request){
    	
    	agentService.deleteByIds(ids);
    	return JsonResult.DELETE_SUCCESS.toString();
    }       
	
    protected Map getQueryMap(Agent agent)
    {
    	Map<String, Object> conditions = new HashMap();
    	
		conditions.put("groupId", agent.getGroupId());		
		conditions.put("version", agent.getVersion());		
		conditions.put("connectKey", agent.getConnectKey());		
		conditions.put("enable", agent.getEnable());		
		conditions.put("ip", agent.getIp());		
		conditions.put("name", agent.getName());		
		conditions.put("createrId", agent.getCreaterId());		
		conditions.put("createrName", agent.getCreaterName());		
		conditions.put("updaterId", agent.getUpdaterId());		
		conditions.put("updaterName", agent.getUpdaterName());		
		conditions.put("createDate", agent.getCreateDate());		
		conditions.put("updateDate", agent.getUpdateDate());		
    	return conditions;
    }

    @Override
    protected IAgentService getMainService() {
        return agentService;
    }

    @Override
    protected String getMainObjName() {
        return "agent";
    }

    @Override
    protected String getViewTitle() {
        return "agent";
    }
}
